/*******************************************************************************
 * Copyright (c) 2008 Sylvain Sicard.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Sylvain Sicard - initial API and implementation
 ******************************************************************************/
package fr.jade.fraclite.orb.names;

import org.objectweb.naming.Binder;
import org.objectweb.naming.Name;
import org.objectweb.naming.NamingContext;
import org.objectweb.naming.NamingException;

public class TcpIpName extends AbstractName {
  
  protected String host;
  
  protected int port;
  
  public TcpIpName(long itfId, String signature, Binder naming, String host, int port){
    super(itfId, signature, naming);
    this.host = host;
    this.port = port;
  }
  
  public TcpIpName(Name name, Binder naming, String signature){
    this(((TcpIpName)name).getItfId(),
         ((TcpIpName)name).getSignature(),
         ((TcpIpName)name).getNaming(),
         ((TcpIpName)name).getHost(),
         ((TcpIpName)name).getPort());
    this.host = ((TcpIpName)name).getHost();
    this.port = ((TcpIpName)name).getPort();
  }
  
  public TcpIpName(byte[] n, Binder naming){
    super(n,naming);
    String name = new String(n);
    String[] values = name.split(NAME_SEPAR);
    host = values[2];
    port = Integer.parseInt(values[3]);
  }
  
  @Override
  public byte[] encode() throws NamingException {
    String name = NAME_SEPAR+host+NAME_SEPAR+String.valueOf(port);
    return super.concat(super.encode(),name.getBytes());
  }

  protected String getHost() {
    return host;
  }

  protected int getPort() {
    return port;
  }

  public boolean equals(Object o){
    if(o instanceof TcpIpName){
      return super.equals(o) &&
             ((TcpIpName)o).getHost().equals(host) &&
             ((TcpIpName)o).getPort() == port;
    }else{
      return false;
    }
  }
}
